package com.jmpt.fangbohui.view;

import com.jmpt.fangbohui.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

/**
 * Created by Deson on 2015/8/4.
 */
public class MyAlertDialog {
    private Context context;
    private Dialog dialog;
    private LinearLayout lLayout_bg;
    private TextView txt_title;
    private LinearLayout dialog_Group;
    private Button btn_neg;
    private Button btn_pos;
    private Display display;
    private TextView tv_day;
    private boolean dateDesc = true;


    public MyAlertDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public MyAlertDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.toast_view_alertdialog, null);

        // 获取自定义Dialog布局中的控件
        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);   // 决定对话框的实际大小
        txt_title = (TextView) view.findViewById(R.id.txt_title);
        dialog_Group = (LinearLayout) view.findViewById(R.id.dialog_Group);
        btn_neg = (Button) view.findViewById(R.id.btn_neg);   //取消
        btn_pos = (Button) view.findViewById(R.id.btn_pos);   // 确定
        tv_day = (TextView) view.findViewById(R.id.tv_day);    // 用来显示   2015年3月15日 星期天

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.85), LayoutParams.WRAP_CONTENT));

        return this;
    }

    public MyAlertDialog setTitle(String title) {

        if ("".equals(title)) {
            txt_title.setText("标题");
        } else {
            txt_title.setText(title);
        }
        return this;
    }


    public MyAlertDialog setDateDescVisible(boolean yes) {
        this.dateDesc = yes;
        return this;
    }

    /**
     * 设置日期和今天是星期几
     *
     * @param day
     */
    public void setDay(String day) {
        if (TextUtils.isEmpty(day)) {
            return;
        }
        tv_day.setText(day);
    }


    public MyAlertDialog setView(View view) {
        if (view == null) return null;

        dialog_Group.addView(view,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        return this;
    }

    public MyAlertDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public MyAlertDialog setPositiveButton(String text, final OnClickListener listener) {
        if ("".equals(text)) {
            btn_pos.setText("确定");
        } else {
            btn_pos.setText(text);
        }
        btn_pos.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public MyAlertDialog setNegativeButton(String text,
                                           final OnClickListener listener) {
        if ("".equals(text)) {
            btn_neg.setText("取消");
        } else {
            btn_neg.setText(text);
        }
        btn_neg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                dialog.dismiss();
            }
        });
        return this;
    }

    public void show() {
        if (dateDesc) {
            tv_day.setVisibility(View.VISIBLE);
        } else {
            tv_day.setVisibility(View.GONE);
        }

        dialog.show();
    }

    public void dismiss() {
        dialog.dismiss();

    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener2) {
        dialog.setOnDismissListener(listener2);
    }


}
